package cn.bgs.service.impl;

import cn.bgs.common.R;
import cn.bgs.pojo.HomeStyle;
import cn.bgs.mapper.HomeStyleMapper;
import cn.bgs.service.HomeStyleService;
import cn.bgs.vo.RandomUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 家乡风貌表 服务实现类
 * </p>
 *
 * @author Mr.Zhao
 * @since 2022-08-08
 */
@Service
public class HomeStyleServiceImpl extends ServiceImpl<HomeStyleMapper, HomeStyle> implements HomeStyleService {

    @Autowired
    private HomeStyleMapper homeStyleMapper;

    @Override
    public R findHomeStyle(String homeStyTitle,String homeStyFrom) {

        List<HomeStyle> homeStyleList = homeStyleMapper.findHomeStyle(homeStyTitle, homeStyFrom);

        return R.ok().data("homeStyleList",homeStyleList);
    }

    @Override
    public R findHomeStyleId(Integer id) {

        HomeStyle homeStyle = homeStyleMapper.findHomeStyleId(id);

        return R.ok().data("homeStyle",homeStyle);
    }

    @Override
    public R addHomeStyle(HomeStyle homeStyle) {

        homeStyle.setHomeStyNumber(RandomUtil.getSixBitRandom());

        homeStyle.setDeleteFlag(0);

        Integer i = homeStyleMapper.insert(homeStyle);

        if (i!=0){
            return R.ok();
        }else {
            return R.error().data("error","新增失败");
        }
    }

    @Override
    public R updateHomeStyle(HomeStyle homeStyle) {

        Integer i = homeStyleMapper.updateById(homeStyle);

        if (i!=0){
            return R.ok();
        }else {
            return R.error().data("error","编辑失败");
        }
    }

    @Override
    public R delHomeStyle(Integer id) {

        HomeStyle homeStyle = homeStyleMapper.selectById(id);

        homeStyle.setDeleteFlag(1);

        Integer i = homeStyleMapper.updateById(homeStyle);

        if (i!=0){
            return R.ok();
        }else {
            return R.error().data("error","删除失败");
        }
    }
}
